package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.common.core.domain.model.LoginAppUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.AppArtifact;
import com.ruoyi.system.mapper.AppArtifactMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AppFavoriteMapper;
import com.ruoyi.system.domain.AppFavorite;
import com.ruoyi.system.service.IAppFavoriteService;

/**
 * 收藏Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-29
 */
@Service
public class AppFavoriteServiceImpl implements IAppFavoriteService 
{
    @Autowired
    private AppFavoriteMapper appFavoriteMapper;
    @Autowired
    private AppArtifactMapper appArtifactMapper;

    /**
     * 查询收藏
     * 
     * @param favoriteId 收藏主键
     * @return 收藏
     */
    @Override
    public AppFavorite selectAppFavoriteByFavoriteId(Long favoriteId)
    {
        return appFavoriteMapper.selectAppFavoriteByFavoriteId(favoriteId);
    }

    /**
     * 查询收藏列表
     * 
     * @param appFavorite 收藏
     * @return 收藏
     */
    @Override
    public List<AppFavorite> selectAppFavoriteList(AppFavorite appFavorite)
    {
        return appFavoriteMapper.selectAppFavoriteList(appFavorite);
    }

    /**
     * 新增收藏
     * 
     * @param appFavorite 收藏
     * @return 结果
     */
    @Override
    public int insertAppFavorite(AppFavorite appFavorite)
    {
        LoginAppUser loginAppUser = SecurityUtils.getLoginAppUser();
        if (Objects.isNull(loginAppUser)) {
            throw new ServiceException("收藏失败，请先登录");
        }
        AppUser appUser = loginAppUser.getAppUser();
        if (Objects.isNull(appUser)) {
            throw new ServiceException("收藏失败，用户不存在");
        }
        AppArtifact artifact = appArtifactMapper.selectAppArtifactByArtifactId(appFavorite.getArtifactId());
        if (Objects.isNull(artifact)) {
            throw new ServiceException("收藏失败，该文物不存在");
        }
        appFavorite.setArtifactName(artifact.getArtifactName());
        appFavorite.setPhotoUrl(artifact.getPhotoUrl());
        appFavorite.setUserId(appUser.getUserId());
        appFavorite.setNickName(appUser.getNickName());
        appFavorite.setEmail(appUser.getEmail());
        appFavorite.setMobile(appUser.getMobile());
        appFavorite.setUserName(appUser.getUserName());

        appFavorite.setCreateTime(DateUtils.getNowDate());
        return appFavoriteMapper.insertAppFavorite(appFavorite);
    }

    /**
     * 修改收藏
     * 
     * @param appFavorite 收藏
     * @return 结果
     */
    @Override
    public int updateAppFavorite(AppFavorite appFavorite)
    {
        return appFavoriteMapper.updateAppFavorite(appFavorite);
    }

    /**
     * 批量删除收藏
     * 
     * @param favoriteIds 需要删除的收藏主键
     * @return 结果
     */
    @Override
    public int deleteAppFavoriteByFavoriteIds(Long[] favoriteIds)
    {
        return appFavoriteMapper.deleteAppFavoriteByFavoriteIds(favoriteIds);
    }

    /**
     * 删除收藏信息
     * 
     * @param favoriteId 收藏主键
     * @return 结果
     */
    @Override
    public int deleteAppFavoriteByFavoriteId(Long favoriteId)
    {
        return appFavoriteMapper.deleteAppFavoriteByFavoriteId(favoriteId);
    }
}
